package com.example.a11



import android.R.id.button2
import android.os.Bundle
import android.view.Gravity
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import android.view.View.OnClickListener
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_main)
//        val b: Button = findViewById(R.id.button2)
//        val textView: TextView = findViewById(R.id.textView)
//        b.setOnClickListener{ it: View? ->
//            textView.text = resources.getString(R.string.clicked)
//            textView.setTextColor(ContextCompat.getColor(this, R.color.red))
//        }

        val linearLayout= LinearLayout(this)
        linearLayout.orientation= LinearLayout.VERTICAL

        val imageView= ImageView(this)
        imageView.layoutParams= ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        imageView.setImageResource(R.drawable.flag) // 设置图片资源
        linearLayout.addView(imageView)

        val textView = TextView(this)
        textView.setTextColor(ContextCompat.getColor(this, R.color.light_blue))
        textView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        textView.textSize = 30F
        textView.gravity = Gravity.CENTER // 文本居中
        textView.text = getString(R.string.textView)

        val layoutParams = ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        layoutParams.setMargins(0, 0, 0, 20) // 设置间距
        textView.layoutParams = layoutParams

        linearLayout.addView(textView) // 加入子节点 textView

        val button = Button(this)
        button.width = ViewGroup.LayoutParams.MATCH_PARENT
        button.height = ViewGroup.LayoutParams.WRAP_CONTENT
        button.text = getString(R.string.button)
        button.background = ContextCompat.getDrawable(this, R.drawable.rounded_button) // 设置背景为圆角矩形
        linearLayout.addView(button)


        setContentView(linearLayout)
        button.setOnClickListener {
            textView.text=getString(R.string.clicked)
            textView.setTextColor(ContextCompat.getColor(this, R.color.red))
            button.text=getString(R.string.purecode)
        }
    }


}


